package top.codestudy.viewtest.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.Target;


import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import top.codestudy.viewtest.R;

public class PictureAdapter extends RecyclerView.Adapter<PictureAdapter.ViewHolder> {
    private static final String TAG = "PictureAdapter";
    private Context mContext;
    int[] imgs = new int[]{R.drawable.golden1,R.drawable.man1,R.drawable.person1,R.drawable.white1,R.drawable.woman1};
    private List<Integer> data = new ArrayList<>();
    private final int count = 60;

    public PictureAdapter( Context context) {
        this.mContext = context;
        init();
    }

    private void init() {
        Random random = new Random(1001);
        for(int i=0;i<count;i++){
            data.add(imgs[random.nextInt(8888)%5]);
        }
        Log.i(TAG, "init: " + data.size());
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.activity_main2, parent, false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Glide.with(mContext)
                .load(data.get(position))
                .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                .into(holder.imageView);
    }

    @Override
    public int getItemCount() {
        return count;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.iv_load);
        }
    }
}
